<template>
  <div id="questionView">
    <a-space style="margin-top: 1vh">
      <a-form :model="searchParams" layout="inline" class="search_input">
        <a-form-item field="title" label="标题">
          <a-input
            v-model="searchParams.title"
            placeholder="请输入你要查询的标题"
          />
        </a-form-item>
        <a-form-item field="tags" label="标签">
          <a-input-tag v-model="searchParams.tags" style="min-width: 220px" />
        </a-form-item>
        <a-form-item>
          <a-button type="primary" @click="doSubmit">查询</a-button>
        </a-form-item>
      </a-form>
    </a-space>
    <a-table
      :columns="columns"
      :data="data"
      :pagination="{
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
      }"
      @page-change="pageChange"
    >
      <template #title="{ record }">
        <a-space>
          <a-link
            @click="goQuestion(record.id)"
            :hoverable="true"
            class="question_link"
            >{{ record.title }}
          </a-link>
        </a-space>
      </template>
      <template #tags="{ record }">
        <a-space>
          <a-tag
            v-for="tag of record.tags"
            :key="tag"
            :color="colors[tag] ?? '#0fc6c2'"
            >{{ tag }}</a-tag
          >
        </a-space>
      </template>
      <template #submitNum="{ record }">
        <a-space> {{ record.submitNum }} </a-space>
      </template>
      <template #acceptNum="{ record }">
        <a-space> {{ record.acceptNum }} </a-space>
      </template>
      <template #createTime="{ record }">
        <a-space>
          {{ moment(record.createTime).format("YYYY-MM-DD") }}
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import {
  Question,
  QuestionControllerService,
  QuestionQueryRequest,
} from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import moment from "moment";
import { colors } from "@/access/GlobalColorEnum";

const columns = [
  {
    title: "题号",
    dataIndex: "id",
  },
  {
    title: "标题",
    slotName: "title",
  },
  {
    title: "标签",
    slotName: "tags",
  },
  {
    title: "提交数",
    slotName: "submitNum",
  },
  {
    title: "通过数",
    slotName: "acceptNum",
  },
  {
    title: "创建时间",
    slotName: "createTime",
  },
];

const searchParams = ref({
  current: 1,
  pageSize: 5,
  title: "",
  tags: [],
} as QuestionQueryRequest);

const data = ref([]) as any;
const total = ref();
const router = useRouter();

// 函数
const loads = async () => {
  const res = await QuestionControllerService.listQuestionVoByPageUsingPost(
    searchParams.value as QuestionQueryRequest
  );
  if (res.code === 0) {
    data.value = res.data.records;
    total.value = res.data.total;
  } else {
    message.error("数据出错" + res.message);
  }
};

onMounted(loads);

// 跳转到对应的题目页面
const goQuestion = (id: number) => {
  router.push({
    path: `/question/detail/${id}`,
  });
};

// 分页函数
const pageChange = (current: number) => {
  searchParams.value = {
    ...searchParams.value,
    current,
  };
  loads();
};
// 查询接口
const doSubmit = () => {
  searchParams.value = {
    ...searchParams.value,
    current: 1,
  };
  loads();
};
</script>

<style scoped>
#questionView {
  min-width: 1200px;
  margin: auto;
  width: 80vw;
}
.question_link:hover {
  color: red;
  text-decoration: underline;
}
.search_input {
  padding: 8px;
}
</style>
